package com.hyhy.hycloud.commons.dto;

import com.hyhy.hycloud.commons.exception.HyCloudRuntimeException;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * @author lanpengfei
 * @date 2018/8/22
 */
@Data
@AllArgsConstructor
public class InvokeResult<T> implements Serializable {
    private String status;
    private String message;
    private T data;

    private final static String STATUS_SUCCESS = "0";
    private final static String MESSAGE_SUCCESS = "SUCCESS";

    public InvokeResult(T data) {
        this.status = STATUS_SUCCESS;
        this.message = MESSAGE_SUCCESS;
        this.data = data;
    }

    public InvokeResult() {
        this(null);
    }

    public static <T> InvokeResult<T> success() {
        return new InvokeResult<>();
    }

    public static <T> InvokeResult<T> success(T data) {
        return new InvokeResult<>(data);
    }

    public static InvokeResult<StackTraceElement> error(HyCloudRuntimeException exception) {
        return new InvokeResult<>(exception.getErrorCode(), exception.getMessage(), exception.getCause() == null ? exception.getStackTrace()[0] : exception.getCause().getStackTrace()[0]);
    }

}
